package com.atguigu.product.service.impl;

import com.atguigu.product.dao.BrandDao;
import com.atguigu.product.dao.CategoryBrandRelationDao;
import com.atguigu.product.dao.CategoryDao;
import com.atguigu.product.entity.BrandEntity;
import com.atguigu.product.entity.CategoryBrandRelationEntity;
import com.atguigu.product.entity.CategoryEntity;
import com.atguigu.product.service.CategoryBrandRelationService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void MySave(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(
                categoryBrandRelationEntity,
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );
    }
    @Override
    public void updateCategory(Long cateId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(cateId);
        categoryBrandRelationEntity.setCatelogName(name);
        this.update(
                categoryBrandRelationEntity,
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",cateId)
        );
    }

    @Override
    public List<BrandEntity> getRelationBrands(Long catId) {
        System.out.println("catId = " + catId);
        List<CategoryBrandRelationEntity> catelogId = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        for (CategoryBrandRelationEntity categoryBrandRelationEntity : catelogId) {
            System.out.println("categoryBrandRelationEntity = " + categoryBrandRelationEntity);
        }
        List<BrandEntity> brandEntityList = catelogId.stream()
                .map(id -> {
                    BrandEntity brandEntity = brandDao.selectById(id.getBrandId());
                    System.out.println(brandEntity.toString());
                    return brandEntity;
                }).collect(Collectors.toList());
        return brandEntityList;
    }


}